package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.associationmanager.bean.*;
import com.example.associationmanager.service.ActivityService;
import com.example.associationmanager.service.FundsService;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Log4j2
public class FundsController {//经费信息数据
    @Autowired
    FundsService fundsService;
    @ResponseBody
    @GetMapping("/select/funds/all")//获取申请的经费项目信息
    public Object getFunds(Integer page , Integer limit, HttpServletRequest request) {
        User user= (User) request.getSession().getAttribute("loginUser");//从session中获取用户属于的社团
        Integer communityNumber= user.getCommunityNumber();
        Page<Funds> fundsPage=new Page<>(page, limit);
        Page<Funds> pageObj = fundsService.page(fundsPage,new QueryWrapper<Funds>().eq("funds_association_id",communityNumber));
        List<Funds> associationAll = pageObj.getRecords();//获取分页查询后的数据
        Msg msg = new Msg(0,"",fundsService.count(), associationAll);
        return msg;
    }
    @ResponseBody
    @PostMapping("/funds/add")//添加单条经费申请
    public Object  FundsAdd(String fundsApplicant,Integer fundsBudget,String fundsPurpose,Integer fundsAssociationId,String fundsApplicantTime) {
        String fundsStatus="申请中";//默认状态，申请中
        Funds funds = new Funds(null,fundsApplicant,fundsBudget,fundsPurpose,fundsAssociationId,fundsApplicantTime,fundsStatus);
        fundsService.save(funds);//向数据库进行经费项目对象的插入
        log.info("日志打印："+"/funds/add单条经费项目添加接口\t"+"项目添加成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
    @ResponseBody
    @GetMapping ("/funds/delete")//删除经费申请
    public Object FundsDelete(@RequestParam("fundsId") String fundsId) {
        fundsService.removeById(fundsId);//删除活动
        log.info("日志打印："+"/funds/delete单条经费项目删除接口\t"+"项目删除成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/funds/apply/pass")//经费项目审批通过
    public Object fundsApplyPass(@RequestParam("fundsId") Integer fundsId) {
        String fundsStatus="已通过";
        Funds funds =new Funds(fundsId,null,null,null,null,null,fundsStatus);
        fundsService.updateById(funds);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @GetMapping ("/funds/apply/no")//经费项目审批不通过
    public Object fundsApplyNo(@RequestParam("fundsId") Integer fundsId) {
        String fundsStatus="已拒绝";
        Funds funds =new Funds(fundsId,null,null,null,null,null,fundsStatus);
        fundsService.updateById(funds);
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
}
